package cn.itheima.com;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.List;

public class Student {
    public static void main(String[] args) throws DocumentException {
        //创建一个解析器对象
        SAXReader saxReader = new SAXReader();
        //将xml文件读取到内存中并返回一个文档对象
        Document Document =  saxReader.read(new File("Day17\\xml\\Student.xml"));
        //利用文档对象得到根标签
        Element rootElement = Document.getRootElement();
        //利用根标签得到一个或者多个子标签
        List<Element> stdeuntelements = rootElement.elements("stduent");

        for (Element stdeuntelement : stdeuntelements) {
            //得到属性值
            Attribute attribute = stdeuntelement.attribute("id");
            String value = attribute.getValue();
            System.out.println(value);
            //得到内容
            Element name = stdeuntelement.element("name");
            String textName = name.getText();
            Element age = stdeuntelement.element("age");
            String textAge = age.getText();
            System.out.println(textName);
            System.out.println(textAge);

        }
    }
}
